extension ByteExtensions on int {
  bool toBit(int offset) {
    return (this >> offset) & 1 == 1;
  }

  int readByteRange(int start, int end) {
    final startMin = start < end ? start : end;
    final model = ((0xff << (7 - (end - start))) & 0xff) >> (7 - end);
    return (this & model) >> startMin;
  }

  int writeBool(bool value, int offset) {
    if (value) {
      return (this | (1 << offset)) & 0xff;
    } else {
      return (this & ~(1 << offset)) & 0xff;
    }
  }

  int writeLow4(int value) {
    return (this & 0xf0) + (value & 0x0f);
  }

  int writeHigh4(int value) {
    final gao = (value << 4) & 0xf0;
    return (gao + (this & 0x0f)) & 0xff;
  }

  bool toBoolean() => this != 0;

  int toUnsigned(int bitLength) => this & ((1 << bitLength) - 1);

  int toUByte() {
    // return this & 0xFF;
    return this.toUnsigned(8);
  }

  int toByte() {
    return this.toSigned(8);
  }
}

extension BoolExtensions on bool {
  int toInt() {
    return this ? 1 : 0;
  }
}
